package com.holyn.guofang.zxing.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.holyn.guofang.MyApplication;
import com.holyn.guofang.R;
import com.holyn.guofang.model.sharepreference.AppSharePreference;
import com.holyn.guofang.model.sharepreference.UserInfoSharePreference;
import com.holyn.guofang.utils.AppUniqueInfoUtil;
import com.holyn.guofang.utils.MD5Uitl;
import com.holyn.guofang.viewcommon.dialog.ToastDialog;
import com.holyn.guofang.zxing.utils.ZxingRESTFul;
import com.holyn.mylibrary.LoadingDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ResultActivity extends Activity implements View.OnClickListener{

	private ImageView mResultImage;
	private Button pcLogin;
	private TextView mResultText,noLogin;
	ToastDialog logoutDialog = null;
	//弹窗
	private LoadingDialog loadingDialog;
	private static final String token = "guofangidenthand075766633966";
//	String device = new AppUniqueInfoUtil(MyApplication.getContext()).getUniqueID();
	String mobile = UserInfoSharePreference.getMobile(this);
	String str = "";
	String result;
	int date;
	String sign;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_result);

		Bundle extras = getIntent().getExtras();
		//设备码获取
		mResultImage = (ImageView) findViewById(R.id.result_image);
		mResultText = (TextView) findViewById(R.id.result_text);
		pcLogin = (Button)findViewById(R.id.ca_authorization);
		noLogin = (TextView)findViewById(R.id.no_authorization);
		if (null != extras) {
			result = extras.getString("result");
			for (int i = 0; i < 3; i++) {
				str = str + (char) (Math.random() * 26 + 'a');
			}
			int num = (int) ((Math.random() * 9 + 1) * 100);
			str=str+num;
			try {
				date = getDate();
			} catch (ParseException e) {
				e.printStackTrace();
			}
			sign = MD5Uitl.getMd5Value("nonce="+str+"&timestamp="+date+"&token="+token);
			mResultText.setText("是否授权PC版商标识别手登录");
		}
		noLogin.setOnClickListener(this);
		pcLogin.setOnClickListener(this);
	}


	private void showCodeLogoutDialog(String info){
		if (logoutDialog == null){
			logoutDialog = new ToastDialog(ResultActivity.this, true, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					ResultActivity.this.finish();
					dialog.dismiss();
				}
			});
			ZxingRESTFul zxingRESTFul = new ZxingRESTFul();
			zxingRESTFul.get(result,mobile,str,sign,1,date,new ZxingRESTFul.IhttpCb() {
				@Override
				public void ok(JSONObject response) {
					Log.e("二维码",result+"设"+mobile+"随"+str+"时"+date+"密"+sign);
					Log.e("fanhu",response.toString());
					try {
						int type = response.getInt("RetCode");
						if (type==1 ){
							logoutDialog.setTvContent(info+response.getString("RetMsg"));
						}
						else if(type==-5){
							logoutDialog.setTvContent("请刷新二维码，然后重新扫描");
						}
						else {
							logoutDialog.setTvContent("写入授权记录失败");
						}

					} catch (JSONException e) {
						logoutDialog.setTvContent("写入授权记录失败");
					}
				}
				@Override
				public void error(String error) {
					logoutDialog.setTvContent("请求失败");
				}
			});
		}
		logoutDialog.show();
	}
	private void showLogoutDialog(String info){
		if (logoutDialog == null){
			logoutDialog = new ToastDialog(ResultActivity.this, true, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					ResultActivity.this.finish();
					dialog.dismiss();
				}
			});
			logoutDialog.setTvContent(info);
		}
		logoutDialog.show();
	}
//	protected void showLoadingDialog(String content){
//		if (loadingDialog == null){
//			loadingDialog = new LoadingDialog(ResultActivity.this);
//		}
//		loadingDialog.setContent(content);
//		loadingDialog.show();
//	}
//
//	protected void closeLoadingDialog(){
//		if (loadingDialog != null){
//			if (loadingDialog.isShowing()){
//				loadingDialog.dismiss();
//			}
//		}
//	}
	public int getDate() throws ParseException {
		String oldday = "1989-12-31";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		//获取当前时间
		Date date = new Date(System.currentTimeMillis());
		Date date1 = simpleDateFormat.parse(oldday);
		int result = (int) (date.getTime()/1000)-(int) (date1.getTime()/1000);
		return result;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.ca_authorization:
				showCodeLogoutDialog("输入验证码:");
				break;
			case R.id.no_authorization:
				showLogoutDialog("确定取消登录？");
				break;
			default:
				break;
		}
	}
}